﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using AEM.Data;
using AEM.Utils;
using Actions = AEM.Data.BLL.Actions;

namespace AEM
{
    /// <summary>
    /// PeakSetting.xaml 的交互逻辑
    /// </summary>
    public partial class PeakSetting : Window
    {
        public PeakSetting()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(PeakSetting_Loaded);
            this.TitleBar.MouseLeftButtonDown += new MouseButtonEventHandler(TitleBar_MouseLeftButtonDown);
            this.btn_add.Click += new RoutedEventHandler(btn_add_Click);
            this.btn_edit.Click += new RoutedEventHandler(btn_edit_Click);
            this.btn_del.Click += new RoutedEventHandler(btn_del_Click);
            this.peekSettingList.SelectionChanged += new SelectionChangedEventHandler(peekSettingList_SelectionChanged);
        }

        void PeakSetting_Loaded(object sender, RoutedEventArgs e)
        {
            if (null != Config.LiftInftList)
                this.liftNum.ItemsSource = from Data.Model.LiftInfo r in Config.LiftInftList
                                           where r.LiftType == (int)Data.Enum.LiftType.电梯
                                           select r;
            this.liftNum.Items.Refresh();
            this.weekday.ItemsSource = StringHelper.EnumToList<DayOfWeek>();
            int[] hour = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 };
            int[] minute = new int[] { 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 };
            this.hfrom.ItemsSource = hour;
            this.hto.ItemsSource = hour;
            this.mfrom.ItemsSource = minute;
            this.mto.ItemsSource = minute;

            ResetUI("");
        }

        void ResetUI(string msg)
        {
            errmsg.Text = msg;
            this.liftNum.SelectedItem = null;
            this.weekday.SelectedItem = null;
            this.hfrom.SelectedIndex = 0;
            this.hto.SelectedIndex = 0;
            this.mfrom.SelectedIndex = 0;
            this.mto.SelectedIndex = 0;
            LoadPeek();
        }

        void LoadPeek()
        {
            int i = 1;
            this.peekSettingList.ItemsSource = null;
            this.peekSettingList.Items.Refresh();
            List<Data.Model.PeakSetting> plist = Actions.PeakSetting.List("");
            if (null == plist) return;
            if (null == Config.LiftInftList) return;
            IEnumerable<PeakSettingItem> list = from r in plist
                                                join Data.Model.LiftInfo l in Config.LiftInftList
                                               on r.LiftNum equals l.LiftNum
                                                orderby r.Weekday, r.LiftNum
                                                select new PeakSettingItem
                                                {
                                                    RowNum = i++,
                                                    ID = r.ID,
                                                    LiftNum = r.LiftNum,
                                                    ShortName = l.ShortName,
                                                    LiftSite = l.LiftSite,
                                                    Weekday = r.Weekday,
                                                    TimeFrom = r.TimeFrom,
                                                    TimeTo = r.TimeTo
                                                };

            this.peekSettingList.ItemsSource = list;
            this.peekSettingList.Items.Refresh();
        }

        private class PeakSettingItem
        {
            public int RowNum { get; set; }
            public int ID { get; set; }
            public int LiftNum { get; set; }
            public string ShortName { get; set; }
            public string LiftSite { get; set; }
            public int Weekday { get; set; }
            public string TimeFrom { get; set; }
            public string TimeTo { get; set; }
        }

        void peekSettingList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PeakSettingItem i = this.peekSettingList.SelectedItem as PeakSettingItem;
            if (null == i) return;
            Data.Model.LiftInfo row = (from r in Config.LiftInftList
                                       where r.LiftNum == i.LiftNum
                                       select r).FirstOrDefault();

            if (null != row) this.liftNum.SelectedItem = row;
            this.weekday.SelectedItem = (DayOfWeek)i.Weekday;
            string[] tmp = i.TimeFrom.Split(':');
            this.hfrom.SelectedItem = tmp[0].toNumeric();
            this.mfrom.SelectedItem = tmp[1].toNumeric();
            tmp = i.TimeTo.Split(':');
            this.hto.SelectedItem = tmp[0].toNumeric();
            this.mto.SelectedItem = tmp[1].toNumeric();
        }

        void btn_del_Click(object sender, RoutedEventArgs e)
        {
            errmsg.Text = "";
            PeakSettingItem i = this.peekSettingList.SelectedItem as PeakSettingItem;
            if (null == i) { errmsg.Text = this.TryFindResource("SelectOne") as string; return; }

            Actions.PeakSetting.DeletePeakResult ret = Actions.PeakSetting.DeletePeak(i.ID);

            switch (ret)
            {
                case Actions.PeakSetting.DeletePeakResult.IsNotExist:
                    errmsg.Text = string.Format(this.TryFindResource("IsNotExist") as string, this.TryFindResource("PeakSettingTitle") as string);
                    break;
                case Actions.PeakSetting.DeletePeakResult.OK:
                    ResetUI(this.TryFindResource("successfuldelete") as string);
                    break;
            }
        }

        void btn_edit_Click(object sender, RoutedEventArgs e)
        {
            errmsg.Text = "";
            PeakSettingItem item = (PeakSettingItem)this.peekSettingList.SelectedItem;
            if (null == item) { errmsg.Text = this.TryFindResource("SelectOne") as string; return; }
            if (null == this.liftNum.SelectedItem) { errmsg.Text = string.Format(this.TryFindResource("AskSelectOne") as string, this.TryFindResource("LiftSite") as string); return; }
            if (null == this.weekday.SelectedItem) { errmsg.Text = string.Format(this.TryFindResource("AskSelectOne") as string, this.TryFindResource("Weekday") as string); return; }

            var start = new TimeSpan((int)this.hfrom.SelectedItem, (int)this.mfrom.SelectedItem, 0);
            var end = new TimeSpan((int)this.hto.SelectedItem, (int)this.mto.SelectedItem, 0);
            if (end <= start) { errmsg.Text = this.TryFindResource("CompareDate") as string; return; }

            Data.Model.PeakSetting model = new Data.Model.PeakSetting();

            model.ID = item.ID;
            model.LiftNum = (this.liftNum.SelectedItem as Data.Model.LiftInfo).LiftNum;
            model.Weekday = (int)(DayOfWeek)this.weekday.SelectedItem;
            model.TimeFrom = ((int)this.hfrom.SelectedItem).ToString("00") + ":" + ((int)this.mfrom.SelectedItem).ToString("00");
            model.TimeTo = ((int)this.hto.SelectedItem).ToString("00") + ":" + ((int)this.mto.SelectedItem).ToString("00");

            Actions.PeakSetting.ModifiyPeakResult ret = Actions.PeakSetting.ModifiyPeak(model);
            switch (ret)
            {
                case Actions.PeakSetting.ModifiyPeakResult.IsNull:
                    errmsg.Text = string.Format(this.TryFindResource("IsNull") as string, this.TryFindResource("PeakSettingTitle") as string);
                    break;
                case Actions.PeakSetting.ModifiyPeakResult.IsNotExist:
                    errmsg.Text = string.Format(this.TryFindResource("IsNotExist") as string, this.TryFindResource("PeakSettingTitle") as string);
                    break;
                case Actions.PeakSetting.ModifiyPeakResult.OK:
                    ResetUI(this.TryFindResource("successfulupdate") as string);
                    Actions.PeakSetting.Reload.Invoke();
                    break;
            }
        }

        void btn_add_Click(object sender, RoutedEventArgs e)
        {
            errmsg.Text = "";
            if (null == this.liftNum.SelectedItem) { errmsg.Text = string.Format(this.TryFindResource("AskSelectOne") as string, this.TryFindResource("LiftSite") as string); return; }
            if (null == this.weekday.SelectedItem) { errmsg.Text = string.Format(this.TryFindResource("AskSelectOne") as string, this.TryFindResource("Weekday") as string); return; }

            var start = new TimeSpan((int)this.hfrom.SelectedItem, (int)this.mfrom.SelectedItem, 0);
            var end = new TimeSpan((int)this.hto.SelectedItem, (int)this.mto.SelectedItem, 0);
            if (end <= start) { errmsg.Text = this.TryFindResource("CompareDate") as string; return; }

            AEM.Data.Model.PeakSetting model = new Data.Model.PeakSetting();

            model.LiftNum = (this.liftNum.SelectedItem as Data.Model.LiftInfo).LiftNum;
            model.Weekday = (int)(DayOfWeek)this.weekday.SelectedItem;
            model.TimeFrom = ((int)this.hfrom.SelectedItem).ToString("00") + ":" + ((int)this.mfrom.SelectedItem).ToString("00");
            model.TimeTo = ((int)this.hto.SelectedItem).ToString("00") + ":" + ((int)this.mto.SelectedItem).ToString("00");

            Actions.PeakSetting.AddPeakResult ret = Actions.PeakSetting.AddPeak(model);
            switch (ret)
            {
                case Actions.PeakSetting.AddPeakResult.IsNull:
                    errmsg.Text = string.Format(this.TryFindResource("IsNull") as string, this.TryFindResource("PeakSettingTitle") as string);
                    break;
                case Actions.PeakSetting.AddPeakResult.IsExist:
                    errmsg.Text = string.Format(this.TryFindResource("AlreadyExists") as string, this.TryFindResource("PeakSettingTitle") as string);
                    break;
                case Actions.PeakSetting.AddPeakResult.OK:
                    ResetUI(this.TryFindResource("successfuladd") as string);
                    Actions.PeakSetting.Reload.Invoke();
                    break;
            }
        }

        void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void sys_button_close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void sys_button_min_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }
    }
}
